package org.example.jedis.transaction;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.List;

/**
 * @author kylin
 * @date 2020/4/18
 * <p>
 * 事务的四大命令MULTI EXEC DISCARD WATCH
 */
public class TestTransaction {
    public static void main(String[] args) {
        // 监视trxkey，然后等待线程2执行了再支持，那么线程1 会报错
        new Thread() {
            public void run() {
                Jedis jedis = new Jedis("192.168.165.7", 6379);
                String watch = jedis.watch("trxkey");
                System.out.println("method1线程[" + Thread.currentThread().getName() + "]watch结果：" + watch);
                Transaction multi = jedis.multi();
                multi.set("trxkey", "2673-thread1");
                // 让Thread2先执行完
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                List<Object> exec = multi.exec();
                System.out.println("method1执行结果：" + exec);
                jedis.unwatch();
            }
        }.start();

        // 和上面一样，也是监视，直接执行，会成功
        new Thread() {
            public void run() {
                Jedis jedis = new Jedis("192.168.165.7", 6379);
                String watch = jedis.watch("trxkey");
                System.out.println("method2线程[" + Thread.currentThread().getName() + "]watch结果：" + watch);
                Transaction multi = jedis.multi();
                multi.set("trxkey", "2673-thread2");
                List<Object> exec = multi.exec();
                System.out.println("method2执行结果：" + exec);
            }
        }.start();

    }


}
